#pragma once
#include "Com.hpp"

class Init
{
public:
    Init() 
    {
        int n = ::mkfifo(gfilename.c_str(),mode);
        if (n < 0)
        {
            std::cerr << "mkfifo error" << std::endl;
        }
        std::cout << "mkfifo success" << std::endl;
    }
    ~Init()
    {
        int n  = ::unlink(gfilename.c_str());
                if (n < 0)
        {
            std::cerr << "unlink error" << std::endl;
        }
        std::cout << "unlink success" << std::endl;
    }
};

Init init;

class Server
{
public:
    Server() :_fd(gdefaultId)
    {}
    bool OpenPipe()
    {
        _fd = open(gfilename.c_str(),O_RDONLY);
        if (_fd < 0)
        {
            std::cerr << "open error" << std::endl;
            return false;
        }
        return true;
    }
    //输出型参数
    int RecivePipe(std::string* out)
    {
        char buffer[gsize];
        int n = ::read(_fd,buffer,sizeof(buffer)-1);
        if (n > 0)
        {
            buffer[n] = 0;
            *out = buffer;
        }
        return n;
    }
    void ClosePipe()
    {
        if (_fd >= 0)
            ::close(_fd);
    }
    ~Server()
    {}
private:
    int _fd;
};